<?php

class Group{
	private $blog;
	private $con;
	private $table;
	
	public function __construct(&$blog){
		$this->blog 	=& $blog;
		$this->con 		=& $blog->con;
		$this->table 	= $this->blog->prefix.'group';
	}
	
	//Manage customers groups
	public function create($group_name){
		$cur = $this->con->openCursor($this->table);
		$cur->group_name = (string) $group_name;
		$strReq = 'SELECT MAX(group_id) FROM '.$this->table;
		$rs = $this->con->select($strReq);
		$cur->group_id = (integer) $rs->f(0) + 1;
		$cur->insert();
	}
	
	public function delete($gid){
		global $core;
		
		if($id!=0){
			if($this->con->driver()=='mysql'){
				$customer = New Customer($this->blog);
				$rs = $customer->get_byGroup($gid);
				while($rs->fetch()){
					$customer->set_fields(array('group_id'=>0),$rs->customer_id);
					$customer->update();
				}
			}
			$this->con->execute('DELETE FROM '.$this->table.' WHERE (group_id='.$id.') LIMIT 1');
		}else{
			$core->error->add('Vous ne pouvez supprimer la catégorie par défaut');
		}
	}
	
	public function move_UserToGroup($user_id,$group_cur,$group_upd=0){
		
	}
	
	public function get_groupName($group_id){
		$rs = $this->con->select('SELECT group_name FROM '.$this->table.' WHERE group_id=\''.$group_id.'\';');
		if ($rs->isEmpty()) {return(array());}
		return $rs->group_name;
	}
	
	public function get_listGroup(){
		$rs = $this->con->select('SELECT group_id, group_name FROM '.$this->table.' ORDER BY group_id ASC;');
		if ($rs->isEmpty()) {return(array());}
		
		while( $rs->fetch() ){
			$group[] = array(	'group_id' => $rs->group_id,
								'group_name'=>$rs->group_name
							);
		}
		return $group;
	}
}

?>